﻿#include "TurnTableCodec.h"

#include "SerialPortManager.h"
#include "LogSaver.h"

TurnTableCodec::TurnTableCodec(QObject* parent) :QObject(parent)//构造函数
{
  timer_sendInstruction.start(timeInterval_sendInstruction);
  connect(&timer_sendInstruction, &QTimer::timeout, this, &TurnTableCodec::slot_timer_sendInstruction_timeOut);
}
TurnTableCodec::~TurnTableCodec() {
  timer_sendInstruction.disconnect();
  disconnect();
}

bool TurnTableCodec::sendInstruction(TurnTableInstructionFlag instructionFlag, float arg1, float arg2, float arg3)
{
  if (!serialPortManager->isListening())
    {
      logSaver->saveLog(QStringLiteral("%1：转台串口未打开，无法发送转台指令").arg(objectName()), LogSaver::Debug);
      return false;
    }
  switch (instructionFlag)
    {
    case TurnTableCodec::TurnTable_Connect:
      logSaver->saveLog(QStringLiteral("%1：发送连接转台指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_NoAxleNum, TurnTable_Connect));
      emit signal_turnTableConnectionState(State_Connecting);
      emit signal_instructionEcho(TurnTable_Connect, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_Disconnect:
      logSaver->saveLog(QStringLiteral("%1：发送断开转台指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_NoAxleNum, TurnTable_Disconnect));
      emit signal_turnTableConnectionState(State_Disconnected);
      emit signal_instructionEcho(TurnTable_Disconnect, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_SetLoad:
      logSaver->saveLog(QStringLiteral("%1：发送设置转台负载模式指令，负载模式为：%2").arg(objectName()).arg((int)arg1), LogSaver::Information);
      sendInstructionController(code(TurnTable_NoAxleNum, TurnTable_SetLoad, arg1));
      emit signal_instructionEcho(TurnTable_SetLoad, arg1, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_PowerOn:
      logSaver->saveLog(QStringLiteral("%1：发送转台上电指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_PowerOn));
      emit signal_instructionEcho(TurnTable_PowerOn, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_PowerOff:
      logSaver->saveLog(QStringLiteral("%1：发送转台下电指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_PowerOff));
      emit signal_instructionEcho(TurnTable_PowerOff, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_CloseCircuit:
      logSaver->saveLog(QStringLiteral("%1：发送转台闭合指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_CloseCircuit));
      emit signal_instructionEcho(TurnTable_CloseCircuit, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_OpenCircuit:
      logSaver->saveLog(QStringLiteral("%1：发送转台闲置指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_OpenCircuit));
      emit signal_instructionEcho(TurnTable_OpenCircuit, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_Stop:
      logSaver->saveLog(QStringLiteral("%1：发送转台停止指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_Stop));
      emit signal_instructionEcho(TurnTable_Stop, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_EmergencyStop:
      logSaver->saveLog(QStringLiteral("%1：发送转台急停指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_EmergencyStop));
      emit signal_instructionEcho(TurnTable_EmergencyStop, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_SearchZero:
      logSaver->saveLog(QStringLiteral("%1：发送转台寻零指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_SearchZero));
      emit signal_instructionEcho(TurnTable_SearchZero, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_MoveToZero:
      logSaver->saveLog(QStringLiteral("%1：发送转台回零指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_MoveToZero));
      emit signal_instructionEcho(TurnTable_MoveToZero, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_SetPositionMode:
      if (((arg1 >= turnTablePositionLowerLimit &&
            arg1 <= turnTablePositionUpperLimit) ||
           (arg1 >= -turnTablePositionUpperLimit &&
            arg1 <= -turnTablePositionLowerLimit)) &&
          arg2 >= turnTableRateLowerLimit &&
          arg2 <= turnTableRateUpperLimit &&
          arg3 >= turnTableAccelerationLowerLimit &&
          arg3 <= turnTableAccelerationUpperLimit) //如果参数没有超限则发送指令
        {
          logSaver->saveLog(QStringLiteral("%1：发送设置转台位置模式指令，位置：%2，速率：%3，加速度：%4").arg(objectName()).arg(arg1).arg(arg2).arg(arg3), LogSaver::Information);
          sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_SetPositionMode, arg1, arg2, arg3));
          emit signal_instructionEcho(TurnTable_SetPositionMode, arg1, arg2, arg3);
          return true;
        }
      else//如果参数超限则发送错误
        {
          logSaver->saveLog(QStringLiteral("%1：错误，转台位置模式参数超限，位置：%2，速率：%3，加速度：%4").arg(objectName()).arg(arg1).arg(arg2).arg(arg3), LogSaver::Critical);
          emit signal_turnTableError(Error_PositionModeOverLimited);
          return false;
        }
      break;
    case TurnTableCodec::TurnTable_SetRateMode:
      if (((arg1 >= turnTableRateLowerLimit&&
            arg1 <= turnTableRateUpperLimit) ||
           (arg1 >= -turnTableRateUpperLimit &&
            arg1 <= -turnTableRateLowerLimit)) &&
          arg2 >= turnTableAccelerationLowerLimit&&
          arg2 <= turnTableAccelerationUpperLimit) //如果参数超限则发送错误
        {
          logSaver->saveLog(QStringLiteral("%1：发送设置转台位置模式指令，速率：%2，加速度：%3").arg(objectName()).arg(arg1).arg(arg2), LogSaver::Information);
          sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_SetRateMode, arg1, arg2, 0));
          emit signal_instructionEcho(TurnTable_SetRateMode, arg1, arg2, 0);
          return true;
        }
      else
        {
          logSaver->saveLog(QStringLiteral("%1：错误，转台速率模式参数超限，速率：%2，加速度：%3").arg(objectName()).arg(arg1).arg(arg2), LogSaver::Critical);
          emit signal_turnTableError(Error_RateModeOverLimited);
          return false;
        }
      break;
    case TurnTableCodec::TurnTable_Run:
      logSaver->saveLog(QStringLiteral("%1：发送转台运行指令，运行模式：%2").arg(objectName()).arg((int)arg1), LogSaver::Information);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_Run, arg1));
      emit signal_instructionEcho(TurnTable_Run, arg1, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_CurrentState:
      logSaver->saveLog(QStringLiteral("%1：发送查询转台当前转台指令").arg(objectName()), LogSaver::Debug);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_CurrentState));
      //emit signal_instructionEcho(TurnTable_CurrentState,0,0,0);
      return true;
      break;
    case TurnTableCodec::TurnTable_SearchZeroState:
      logSaver->saveLog(QStringLiteral("%1：发送查询转台寻零状态指令").arg(objectName()), LogSaver::Debug);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_SearchZeroState));
      emit signal_instructionEcho(TurnTable_SearchZeroState, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_RunningState:
      logSaver->saveLog(QStringLiteral("%1：发送查询转台运行状态指令").arg(objectName()), LogSaver::Debug);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_RunningState));
      //emit signal_instructionEcho(TurnTable_RunningState,0,0,0);
      return true;
      break;
    case TurnTableCodec::TurnTable_RunningMode:
      logSaver->saveLog(QStringLiteral("%1：发送查询转台运行模式指令").arg(objectName()), LogSaver::Debug);
      sendInstructionController(code(TurnTable_AxleNumOne, TurnTable_RunningMode));
      //emit signal_instructionEcho(TurnTable_RunningMode,0,0,0);
      return true;
      break;
    case TurnTableCodec::Thermostat_Connect:
      logSaver->saveLog(QStringLiteral("%1：发送连接温箱指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_NoAxleNum, Thermostat_Connect));
      emit signal_instructionEcho(Thermostat_Connect, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::Thermostat_Disconnect:
      logSaver->saveLog(QStringLiteral("%1：发送断开温箱指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_NoAxleNum, Thermostat_Disconnect));
      emit signal_instructionEcho(Thermostat_Disconnect, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::Thermostat_TargetTemperature:
      logSaver->saveLog(QStringLiteral("%1：发送设置温箱目标温度指令，目标温度：%2").arg(objectName()).arg(arg1), LogSaver::Information);
      sendInstructionController(code(TurnTable_NoAxleNum, Thermostat_TargetTemperature, arg1, 0, 0));
      emit signal_instructionEcho(Thermostat_TargetTemperature, arg1, 0, 0);
      return true;
      break;
    case TurnTableCodec::Thermostat_CurrentTemperature:
      logSaver->saveLog(QStringLiteral("%1：发送查询温箱当前温度指令").arg(objectName()), LogSaver::Debug);
      sendInstructionController(code(TurnTable_NoAxleNum, Thermostat_CurrentTemperature));
      //emit signal_instructionEcho(Thermostat_CurrentTemperature,0,0,0);
      return true;
      break;
    case TurnTableCodec::Thermostat_TemperatureRate:
      logSaver->saveLog(QStringLiteral("%1：发送设置温箱温度速率指令，温度速率：%2").arg(objectName()).arg(arg1), LogSaver::Information);
      sendInstructionController(code(TurnTable_NoAxleNum, Thermostat_TemperatureRate, arg1, 0, 0));
      emit signal_instructionEcho(Thermostat_TemperatureRate, arg1, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_QuitConsole:
      logSaver->saveLog(QStringLiteral("%1：发送退出转台控制台指令").arg(objectName()), LogSaver::Information);
      sendInstructionController(code(TurnTable_NoAxleNum, TurnTable_QuitConsole));
      emit signal_instructionEcho(TurnTable_QuitConsole, 0, 0, 0);
      return true;
      break;
    case TurnTableCodec::TurnTable_Error:
      logSaver->saveLog(QStringLiteral("%1：不能发送转台错误反馈").arg(objectName()), LogSaver::Warning);
      return false;
    default:
      logSaver->saveLog(QStringLiteral("%1：不能发送无法识别的指令").arg(objectName()), LogSaver::Warning);
      return false;
    }
  return false;
}

void TurnTableCodec::slot_decode(QByteArray instruction)
{
  logSaver->saveLog(QStringLiteral("%1：收到转台反馈的数据：%2").arg(objectName()).arg((QString)instruction.toHex()), LogSaver::Debug);
  dataBuffer.append(instruction);
  decode();
}
void TurnTableCodec::decode()
{
  while ((dataBuffer.size() / dataPackageSize) > 0)
    {
      if (checkDataPackageTurnTableDeviceNum() && checkCheckSummation())
        {
          decode_body();
        }
    }
  return;
}
void TurnTableCodec::decode_body()
{
  switch (dataBuffer.at(1))
    {
    case TurnTable_NoAxleNum:
      switch (dataBuffer.at(2))
        {
        case TurnTable_Connect:
          logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台已连接").arg(objectName()), LogSaver::Information);
          emit signal_turnTableConnectionState(State_Connected);
          break;
        case TurnTable_Disconnect:
          logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台已断开").arg(objectName()), LogSaver::Information);
          emit signal_turnTableConnectionState(State_Disconnected);
          break;
        case Thermostat_CurrentTemperature:
          logSaver->saveLog(QStringLiteral("%1：收到转台反馈：当前温箱温度").arg(objectName()), LogSaver::Debug);
          emit signal_currentThermostatTemperature(dataBodyToFloat(1));
          break;
        default:
          break;
        }
      break;
    case TurnTable_AxleNumOne:
      switch (dataBuffer.at(2))
        {
        case TurnTable_CurrentState:
          logSaver->saveLog(QStringLiteral("%1：收到转台反馈：当前转台状态").arg(objectName()), LogSaver::Debug);
          emit signal_turnTableCurrentState(dataBodyToFloat(1), dataBodyToFloat(2));
          break;
        case TurnTable_SearchZeroState:
          switch (dataBodyToInt(1))
            {
            case State_NotSearch:
              logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台未寻零").arg(objectName()), LogSaver::Debug);
              emit signal_turnTableSearchZeroState(State_NotSearch);
              break;
            case State_Searching:
              logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台寻零中").arg(objectName()), LogSaver::Debug);
              emit signal_turnTableSearchZeroState(State_Searching);
              break;
            case State_Searched:
              logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台已寻零").arg(objectName()), LogSaver::Debug);
              emit signal_turnTableSearchZeroState(State_Searched);
              break;
            default:
              break;
            }
          break;
        case TurnTable_RunningState:
          checkRunningState();
          break;
        case TurnTable_RunningMode:
          switch (dataBodyToInt(1))
            {
            case Mode_PositionMode:
              logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台处于位置模式").arg(objectName()), LogSaver::Debug);
              emit signal_turnTableRunningMode(Mode_PositionMode);
              break;
            case Mode_RateMode:
              logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台处于速率模式").arg(objectName()), LogSaver::Debug);
              emit signal_turnTableRunningMode(Mode_RateMode);
              break;
            default:
              break;
            }
          break;
        case TurnTable_Error:
          switch (dataBuffer.at(3))
            {
            case Error_ProcessState:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台过程状态错误").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_ProcessState);
              break;
            case Error_AxleState:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台轴状态错误").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_AxleState);
              break;
            case Error_AxleNum:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台轴号错误").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_AxleNum);
              break;
            case Error_CheckSummation:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台检查和错误").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_CheckSummation);
              break;
            case Error_TurnTableAddress:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台地址错误").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_TurnTableAddress);
              break;
            case Error_Load:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台负载错误").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_Load);
              break;
            case Error_PositionModeOverLimited:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台位置模式超限").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_PositionModeOverLimited);
              break;
            case Error_RateModeOverLimited:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台速率模式超限").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_RateModeOverLimited);
              break;
            case Error_OtherUnfitCondition:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台其他不符合限制条件的错误").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_OtherUnfitCondition);
              break;
            case Error_ThermostatTargetTemperatureFailed:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台设置目标温度失败").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_ThermostatTargetTemperatureFailed);
              break;
            case Error_ThermostatTargetTemperatureOverLimited:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台设置目标温度超限").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_ThermostatTargetTemperatureOverLimited);
              break;
            case Error_ThermostatFeedbackError:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：温箱反馈帧错误").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_ThermostatFeedbackError);
              break;
            case Error_Connection:
              logSaver->saveLog(QStringLiteral("%1：收到转台错误：转台连接错误").arg(objectName()), LogSaver::Critical);
              emit signal_turnTableError(Error_Connection);
              break;
            default:
              break;
            }
          break;
        default:
          break;
        }
      break;
    default:
      break;
    }
  dataBufferDropData(dataPackageSize);
}
void TurnTableCodec::checkRunningState()
{
  int state = dataBodyToInt(1);
  if (state & 0x00000001) {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：目标位置已达到").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableTargetPositionState(State_Reached);
    }
  else {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：目标位置未达到").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableTargetPositionState(State_NotReached);
    }
  if (state & 0x00000002) {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：目标速率已达到").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableTargetRateState(State_Reached);
    }
  else {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：目标速率未达到").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableTargetRateState(State_NotReached);
    }
  if (state & 0x00000004) {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台已停止").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableTargetStop(State_Reached);
    }
  else {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台未停止").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableTargetStop(State_NotReached);
    }
  if (state & 0x00000008) {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台已上电").arg(objectName()), LogSaver::Debug);
      emit signal_turnTablePowerState(State_PowerOn);
    }
  else {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台已下电").arg(objectName()), LogSaver::Debug);
      emit signal_turnTablePowerState(State_PowerOff);
    }
  if (state & 0x00000010) {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台已闭合").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableCircuitState(State_CircuitClosed);
    }
  else {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台已闲置").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableCircuitState(State_CircuitOpened);
    }
  if (state & 0x00000020) {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：功放故障").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableAmplifierState(State_AmplifierBroken);
    }
  else {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：功放正常").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableAmplifierState(State_AmplifierNormal);
    }
  if (state & 0x00000040) {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台运行中").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableRunningState(State_TurnTableRunning);
    }
  else {
      logSaver->saveLog(QStringLiteral("%1：收到转台反馈：转台未运行").arg(objectName()), LogSaver::Debug);
      emit signal_turnTableRunningState(State_TurnTableStoped);
    }
}
bool TurnTableCodec::checkDataPackageTurnTableDeviceNum()
{
  if (dataBuffer.at(0) == (char)TurnTable_DeviceNum)
    {
      return true;
    }
  else
    {
      dataBufferDropData(1);
      return false;
    }
}

void TurnTableCodec::dataBufferDropData(int byteCount)
{
  dataBuffer.remove(0, byteCount);
}
void TurnTableCodec::addCheckSummation(QByteArray* instruction)//添加检查和
{
  int len = 15/*instruction->size()*/;
  char total = 0x00;
  for (int i = 0; i < len; i++) {
      char temp = instruction->at(i);
      total += temp;
    }
  total = ~total;
  total += 1;
  instruction->append(total);
  return;
}
void TurnTableCodec::setZeroInBehind(QByteArray* instruction)//将不使用的位置全部置零
{
  int begin = instruction->size();
  int end = 15;
  for (int i = begin; i < end; i++) {
      instruction->append((char)0x00);
    }
  return;
}
void TurnTableCodec::addInstructionHead(QByteArray* instruction, char axleNum, char instructionFlag)//添加指令头
{
  instruction->append(TurnTable_DeviceNum);//第一字节：设备号
  instruction->append(axleNum);//第二字节：轴号
  instruction->append(instructionFlag);//第三字节：命令字
  return;
}
void TurnTableCodec::addDataBody(QByteArray* instruction, int data)//添加数据体（整形）
{
  char temp[4];
  intToChar4(data, temp);
  for (int i = 0; i < 4; i++) {//将int的四个字节写入
      instruction->append(temp[i]);
    }
  return;
}
void TurnTableCodec::addDataBody(QByteArray* instruction, float data)//添加数据体（浮点型）
{
  if (data == 0) {
      char temp = 0x00;
      for (int i = 0; i < 4; i++) {
          instruction->append(temp);
        }
      return;
    }
  int data1 = data * 100000;
  addDataBody(instruction, (int)data1);
  return;
}
bool TurnTableCodec::checkCheckSummation()//检查检查和
{
  char total = 0;
  for (int i = 0; i < dataPackageSize; i++) {
      total += dataBuffer.at(i);
    }
  if (total == 0) {
      return true;
    }
  else {
      dataBufferDropData(dataPackageSize);
      return false;
    }
}
void TurnTableCodec::sendInstructionController(QByteArray instruction)
{
  instructionQueue.append(instruction);
}

QByteArray TurnTableCodec::code(char axleNum, char instructionFlag, float data1, float data2, float data3)
{
  QByteArray instruction;
  addInstructionHead(&instruction, axleNum, instructionFlag);//装入指令头
  addDataBody(&instruction, data1);//装入float数据1
  addDataBody(&instruction, data2);//装入float数据2
  addDataBody(&instruction, data3);//装入float数据3
  addCheckSummation(&instruction);//装入检查和
  return instruction;
}
QByteArray TurnTableCodec::code(char axleNum, char instructionFlag)
{
  return code(axleNum, instructionFlag, 0);
}
QByteArray TurnTableCodec::code(char axleNum, char instructionFlag, int data1)
{
  QByteArray instruction;
  addInstructionHead(&instruction, axleNum, instructionFlag);//装入指令头
  addDataBody(&instruction, data1);//装入int数据1
  setZeroInBehind(&instruction);//剩余位全部置零
  addCheckSummation(&instruction);//装入检查和
  return instruction;
}
int TurnTableCodec::dataBodyToInt(int dataBodyNum)
{
  char temp[4];
  for (int i = 0; i < 4; i++) {
      temp[i] = dataBuffer.at(4 * dataBodyNum - 1 + i);
    }
  return char4ToInt(temp);
}
float TurnTableCodec::dataBodyToFloat(int dataBodyNum)
{
  char temp[4];
  for (int i = 0; i < 4; i++) {
      temp[i] = dataBuffer.at(4 * dataBodyNum - 1 + i);
    }
  float result = (float)(char4ToInt(temp) * 0.00001);
  return result;
}
//将转台通讯中的先低后高的四字节数据位转换为整形
int TurnTableCodec::char4ToInt(char* data)
{
  int number = *(int*)data;
  return number;
}

//将整形转换为转台通讯中的先低后高的四字节数据位
char* TurnTableCodec::intToChar4(int data, char* data1)
{
  char* pointer = (char*)&data;
  for (int i = 0; i < 4; i++) {
      data1[i] = pointer[i];
    }
  return data1;
}
void TurnTableCodec::slot_timer_sendInstruction_timeOut()
{
  if (!instructionQueue.isEmpty())
    {//如果指令队列不为空，则发送指令
      emit signal_sendInstruction(instructionQueue.first());
      instructionQueue.removeFirst();
    }
}

float TurnTableCodec::getTurnTablePositionUpperLimit()
{
  return turnTablePositionUpperLimit;
}
void TurnTableCodec::setTurnTablePositionUpperLimit(float turnTablePositionUpperLimit)
{
  this->turnTablePositionUpperLimit; turnTablePositionUpperLimit;
}
float TurnTableCodec::getTurnTablePositionLowerLimit()
{
  return turnTablePositionLowerLimit;
}
void TurnTableCodec::setTurnTablePositionLowerLimit(float turnTablePositionLowerLimit)
{
  this->turnTablePositionLowerLimit = turnTablePositionLowerLimit;
}
float TurnTableCodec::getTurnTableRateUpperLimit()
{
  return turnTableRateUpperLimit;
}
void TurnTableCodec::setTurnTableRateUpperLimit(float turnTableRateUpperLimit)
{
  this->turnTableRateUpperLimit = turnTableRateUpperLimit;
}
float TurnTableCodec::getTurnTableRateLowerLimit()
{
  return turnTableRateLowerLimit;
}
void TurnTableCodec::setTurnTableRateLowerLimit(float turnTableRateLowerLimit)
{
  this->turnTableRateLowerLimit = turnTableRateLowerLimit;
}
float TurnTableCodec::getTurnTableAccelerationUpperLimit()
{
  return turnTableAccelerationUpperLimit;
}
void TurnTableCodec::setTurnTableAccelerationUpperLimit(float turnTableAccelerationUpperLimit)
{
  this->turnTableAccelerationUpperLimit = turnTableAccelerationUpperLimit;
}
float TurnTableCodec::getTurnTableAccelerationLowerLimit()
{
  return turnTableAccelerationLowerLimit;
}
void TurnTableCodec::setTurnTableAccelerationLowerLimit(float turnTableAccelerationLowerLimit)
{
  this->turnTableAccelerationLowerLimit = turnTableAccelerationLowerLimit;
}

SerialPortManager* TurnTableCodec::getSerialPortManager()
{
  return serialPortManager;
}
void TurnTableCodec::setSerialPortManager(SerialPortManager* serialPortManager)
{
  if (Q_NULLPTR!=this->serialPortManager)
    {
      disconnect(serialPortManager, &SerialPortManager::signal_dataReceived, this, &TurnTableCodec::slot_decode);
      disconnect(this, &TurnTableCodec::signal_sendInstruction, serialPortManager, &SerialPortManager::slot_sendData);
    }
  this->serialPortManager = serialPortManager;
  connect(serialPortManager, &SerialPortManager::signal_dataReceived, this, &TurnTableCodec::slot_decode);
  connect(this, &TurnTableCodec::signal_sendInstruction, serialPortManager, &SerialPortManager::slot_sendData);
}
LogSaver* TurnTableCodec::getLogSaver()
{
  return logSaver;
}
void TurnTableCodec::setLogSaver(LogSaver* logSaver)
{
  this->logSaver = logSaver;
}
